1   /*
2    * Copyright (C) 2007 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import com.google.common.annotations.GwtCompatible;
20  
21  import java.util.Collection;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.annotation.Nullable;
26  
27  /**
28   * Basic implementation of the {@link ListMultimap} interface. It's a wrapper
29   * around {@link AbstractMapBasedMultimap} that converts the returned collections into
30   * {@code Lists}. The {@link #createCollection} method must return a {@code
31   * List}.
32   *
33   * @author Jared Levy
34   * @since 2.0 (imported from Google Collections Library)
35   */
36  @GwtCompatible
37  abstract class AbstractListMultimap<K, V>
38      extends AbstractMapBasedMultimap<K, V> implements ListMultimap<K, V> {
39    /**
40     * Creates a new multimap that uses the provided map.
41     *
42     * @param map place to store the mapping from each key to its corresponding
43     *     values
44     */
45    protected AbstractListMultimap(Map<K, Collection<V>> map) {
46      super(map);
47    }
48  
49    @Override abstract List<V> createCollection();
50  
51    @Override
52    List<V> createUnmodifiableEmptyCollection() {
53      return ImmutableList.of();
54    }
55  
56    // Following Javadoc copied from ListMultimap.
57  
58    /**
59     * {@inheritDoc}
60     *
61     * <p>Because the values for a given key may have duplicates and follow the
62     * insertion ordering, this method returns a {@link List}, instead of the
63     * {@link Collection} specified in the {@link Multimap} interface.
64     */
65    @Override public List<V> get(@Nullable K key) {
66      return (List<V>) super.get(key);
67    }
68  
69    /**
70     * {@inheritDoc}
71     *
72     * <p>Because the values for a given key may have duplicates and follow the
73     * insertion ordering, this method returns a {@link List}, instead of the
74     * {@link Collection} specified in the {@link Multimap} interface.
75     */
76    @Override public List<V> removeAll(@Nullable Object key) {
77      return (List<V>) super.removeAll(key);
78    }
79  
80    /**
81     * {@inheritDoc}
82     *
83     * <p>Because the values for a given key may have duplicates and follow the
84     * insertion ordering, this method returns a {@link List}, instead of the
85     * {@link Collection} specified in the {@link Multimap} interface.
86     */
87    @Override public List<V> replaceValues(
88        @Nullable K key, Iterable<? extends V> values) {
89      return (List<V>) super.replaceValues(key, values);
90    }
91  
92    /**
93     * Stores a key-value pair in the multimap.
94     *
95     * @param key key to store in the multimap
96     * @param value value to store in the multimap
97     * @return {@code true} always
98     */
99    @Override public boolean put(@Nullable K key, @Nullable V value) {
100     return super.put(key, value);
101   }
102 
103   /**
104    * {@inheritDoc}
105    *
106    * <p>Though the method signature doesn't say so explicitly, the returned map
107    * has {@link List} values.
108    */
109   @Override public Map<K, Collection<V>> asMap() {
110     return super.asMap();
111   }
112 
113   /**
114    * Compares the specified object to this multimap for equality.
115    *
116    * <p>Two {@code ListMultimap} instances are equal if, for each key, they
117    * contain the same values in the same order. If the value orderings disagree,
118    * the multimaps will not be considered equal.
119    */
120   @Override public boolean equals(@Nullable Object object) {
121     return super.equals(object);
122   }
123 
124   private static final long serialVersionUID = 6588350623831699109L;
125 }